#include "sambaconfigdialog.h"
#include "sambamanager.h"
#include "ui_sambaconfigdialog.h"

#include <QFileDialog>

SambaConfigDialog::SambaConfigDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SambaConfigDialog)
{
    ui->setupUi(this);
    ui->buttonBox->setEnabled(false);

    ui->usershare_acl->addItem("默认权限", "S-1-1-0:F");
}

SambaConfigDialog::~SambaConfigDialog()
{
    delete ui;
}

SambaShare SambaConfigDialog::config() {
    SambaShare share = SambaShare::createShare(ui->sharename->text(), ui->path->text());
    return share;
}

void SambaConfigDialog::on_sharename_textChanged(const QString &arg1)
{
    auto filepath = var_lib_samba_usersahres + arg1;
    QFile file(filepath);
    if (file.exists() || arg1.contains("/") || arg1.isEmpty()) {
        ui->buttonBox->setEnabled(false);
        ui->sharename->setStyleSheet("color:red;");
    } else {
        ui->sharename->setStyleSheet("");
        ui->buttonBox->setEnabled(true);
        if (ui->sharename->text().toLower().compare(arg1) != 0) {
            ui->sharename->setText(arg1.toLower());
        }
        ui->filepath->setText(filepath.toLower());
    }
}


void SambaConfigDialog::on_btn_choose_dir_clicked()
{
    QString dirpath = QFileDialog::getExistingDirectory(this);
    if (dirpath.isEmpty() == false) {
        ui->path->setText(dirpath);
        if (ui->sharename->text().isEmpty()) {
            ui->sharename->setText(dirpath.split("/").last().toLower());
        }
    }
}

